package backend

import (
	"log"
	"runtime"

	"github.com/wailsapp/wails/v3/pkg/application"
	"github.com/wailsapp/wails/v3/pkg/services/notifications"
	"go.uber.org/zap"
)

func (a *App) GetNotifier() *notifications.NotificationService {
	return a.notifier
}

func (a *App) PrepareNotification() error {
	if runtime.GOOS != "darwin" {
		return nil // Windows/Linux 不需要权限
	}
	authorized, err := a.notifier.CheckNotificationAuthorization()
	if err != nil {
		a.log.Error("notifier authorization check failed", zap.Error(err))
	}

	if !authorized {
		if authorized, err = a.notifier.RequestNotificationAuthorization(); err != nil {
			a.log.Error("notifier authorization request failed", zap.Error(err))
		}
		if !authorized {
			a.log.Error("user denied notification permission")
		}
	}
	// 消息分类
	messageCategory := notifications.NotificationCategory{
		ID: "MESSAGE_CATEGORY",
		Actions: []notifications.NotificationAction{
			{ID: "REPLY", Title: "回复"},
			{ID: "DELETE", Title: "删除", Destructive: true},
		},
		HasReplyField:    true,
		ReplyPlaceholder: "输入回复内容...",
		ReplyButtonTitle: "发送",
	}
	a.notifier.RegisterNotificationCategory(messageCategory)

	// 事件分类
	eventCategory := notifications.NotificationCategory{
		ID: "EVENT_CATEGORY",
		Actions: []notifications.NotificationAction{
			{ID: "ACCEPT", Title: "接受"},
			{ID: "DECLINE", Title: "拒绝", Destructive: true},
		},
	}
	a.notifier.RegisterNotificationCategory(eventCategory)
	return nil
}

func (a *App) SendNotification(id, title, body string, data map[string]interface{}, categoryID string) error {
	if a.notifier == nil {
		return nil // 如果通知服务未初始化，直接返回
	}
	notification := notifications.NotificationOptions{
		ID:         id,
		Title:      title,
		Body:       body,
		CategoryID: categoryID,
		Data:       data,
	}

	if err := a.notifier.SendNotification(notification); err != nil {
		a.log.Error("Failed to send notification", zap.Error(err))
		return err
	}
	return nil
}

// 设置通知处理
func (a *App) SetupNotificationHandlers(app *application.App) {
	a.notifier.OnNotificationResponse(func(result notifications.NotificationResult) {
		if result.Error != nil {
			return
		}
		response := result.Response

		// 处理不同类型通知
		switch response.CategoryID {
		case "MESSAGE_CATEGORY":
			a.handleMessageResponse(app, response)
		case "EVENT_CATEGORY":
			a.handleEventResponse(app, response)
		default:
			a.handleDefaultResponse(app, response)
		}
	})
}

// 处理消息通知响应
func (a *App) handleMessageResponse(app *application.App, response notifications.NotificationResponse) {
	switch response.ActionIdentifier {
	case "REPLY":
		log.Printf("用户回复: %s", response.UserText)
	case "DELETE":
		log.Printf("用户删除消息")
	}
}

// 处理事件通知响应
func (a *App) handleEventResponse(app *application.App, response notifications.NotificationResponse) {
	status := "unknown"
	switch response.ActionIdentifier {
	case "ACCEPT":
		status = "accepted"

	case "DECLINE":
		status = "declined"
	}

	log.Printf("事件响应: %s - %s", response.ID, status)
}

// 处理默认通知
func (a *App) handleDefaultResponse(app *application.App, response notifications.NotificationResponse) {
	log.Printf("默认通知响应: %s - %s", response.ID, response.ActionIdentifier)
	// 可以在这里添加更多处理逻辑
}
